<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../../js/vue.js"></script>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        list-style-type: none;
    }

    #app {
        margin: 50px auto;
        width: max-content;
    }

    #app .content {
        width: 400px;
        height: max-content;
        line-height: 40px;
        text-align: center;
        background-color: #000;
        color: #fff;
    }

    .tr {
        overflow: hidden;
    }

    .item {
        float: left;
        width: 100px;
        height: 40px;
        cursor: context-menu;
    }

    .del:hover {
        color: coral;
    }

    .header {
        margin-bottom: 10px;
        position: relative;
    }

    .header input {
        outline: none;
    }

    .age {
        margin: 10px 0;
    }

    .add {
        position: absolute;
        bottom: 30px;
        right: 110px;
    }

    .orderCol {
        margin-right: 20px;
    }
</style>

<body>
    <div id="app">
        <div class="header">
            <div>
                <p>用户名:<input type="text" v-model="user"></p>
                <p class="age">年&emsp;龄:<input type="text" v-model="age"></p>
                <button class="add" @click="add()">添加</button>
                <span class="orderCol">
                    <label>id:<input type="radio" name="orderCol" value="id" v-model="sort"></label>
                    <label>年龄:<input type="radio" name="orderCol" value="age" v-model="sort"></label>
                </span>
                <span class="orderType">
                    <label>升序:<input type="radio" name="orderType" value="asc" v-model="order"></label>
                    <label>降序:<input type="radio" name="orderType" value="desc" v-model="order"></label>
                </span>
            </div>

        </div>
        <div class="content">
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">姓名</div>
                <div class="item">年龄</div>
                <div class="item">操作</div>
            </div>
            <div class="tr" v-for="item in list" :key="item.id">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.user}}</div>
                <div class="item">{{item.age}}</div>
                <div class="item del" @click="del(item.id)">删除</div>
            </div>
        </div>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            user: "",
            age: "",
            sort: "id",
            order: "asc",
            list: [
                {id: 1, user: "a", age: 18},
                {id: 2, user: "b", age: 12},
                {id: 3, user: "c", age: 10},
                {id: 4, user: "d", age: 20}
            ],

        },
        methods: {
            del(id) {
                // console.log(id);
                if (confirm("是否删除该数据？"))
                    this.list = this.list.filter(function (item) {
                        return item.id != id;
                    })
            },
            add() {
                console.log(1);
                if (this.list.some(item => this.user == item.user)) { //用户名不能重复
                    alert("此用户名已存在！");
                    return false;
                }
                if (!this.user && !this.age) return false; //输入框没有内容就不运行

                let id = 1;
                if (this.list.length > 0) { //有数据
                    //取最大的id+1
                    //map里面找到所有的id放到集合中
                    //把集合倒序排列
                    //取第一个
                    id = this.list.map(item => item.id).sort((a, b) => b - a)[0] + 1;
                }

                this.list.push({
                    id,
                    user: this.user,
                    age: this.age,
                })
            },

        },
        watch: {
            sort(newSort) {
                this.list.sort((a, b) => {
                    if (this.order == "asc") {
                        return a[newSort] - b[newSort];
                    } else {
                        return b[newSort] - a[newSort];
                    }
                })
            },
            order(newOrder) {
                this.list.sort((a, b) => {
                    if (newOrder == "asc") {
                        return a[this.sort] - b[this.sort];
                    } else {
                        return b[this.sort] - a[this.sort];
                    }
                })
            }
        }
    })

</script>

</html>